﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProductionManagement.Application.Command.ProOrderCommand;
using ProductionManagement.Domain.Dtos;
using ProductionManagement.Dtos;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace ProductionManagement.Controllers
{
    /// <summary>
    /// 生产订单
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProsduceOrderController : ControllerBase
    {


        IMediator _mediator;

        public ProsduceOrderController(IMediator mediator)
        {
            _mediator = mediator;
        }


        /// <summary>
        /// 添加生产订单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddProOrder(AddProductOrderDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();

            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; //获取用户ID
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.Office_Id = sid;
                command.User_Id = int.Parse(userId);
                command.AddName = name;
                result.Data = await _mediator.Send(new CreateProOrderCommand { AddDto = command });
                result.Code = 200;
                result.Message = "添加成功";
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 获取生产订单列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<SelectOrderDTO>>> GetProduceOrder([FromQuery]SelectProOrderCommand command)
        {
            ApiResult<PageDTO<SelectOrderDTO>> result = new ApiResult<PageDTO<SelectOrderDTO>>();
            try
            {
                result.Data = await _mediator.Send(command);
                result.Code = 200;
                result.Message = "获取成功";
            }            
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }



        /// <summary>
        /// 删除生产订单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> DelProOrder(DelProOrderCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();

            try
            {
               
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.DelName = name;
                result.Data = await _mediator.Send(command);
                result.Code = 200;
                result.Message = "删除成功";
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }



        /// <summary>
        /// 获取生产订单详情
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<FillProOrderDTO>> GetFillProOrder([FromQuery]FillProOrderCommand command)
        {
            ApiResult<FillProOrderDTO> result = new ApiResult<FillProOrderDTO>();
            try
            {
                result.Data = await _mediator.Send(command);
                result.Code = 200;
                result.Message = "获取成功";
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }



        /// <summary>
        /// 添加生产订单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateProOrder(UptOrderDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();

            try
            {
               
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
              
                command.UpdName = name;
                command.UpdTime = DateTime.Now;
                result.Data = await _mediator.Send(new UptProOrderCommand {  Dto = command });
                result.Code = 200;
                result.Message = "修改成功";
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

    }
}
